﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Einstein.Utility
{


    public class RealTimeBoardAPI
    {
        /// <summary>
        /// 默认端口
        /// </summary>
        public static readonly int DefaultPort = 6767;

        #region 打开关闭网络，设置服务器，绑定端口

        /// <summary>
        /// 打开网络 建立通迅
        /// </summary>
        /// <returns>int</returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_open_Net")]
        public static extern int OpenNet();

        /// <summary>
        ///关闭网络 
        /// </summary>
        /// <returns>int</returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Close_Net")]
        public static extern int CloseNet();

        /// <summary>
        /// 绑定通迅端口
        /// </summary>
        /// <param name="szServer">本地网卡的IP地址</param>
        /// <param name="port">本地网络端口</param>
        /// <returns>int</returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Udp_bind")]
        public static extern int BindPort(StringBuilder szServer, int port);

        /// <summary>
        /// 设置服务器
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Set_Server")]
        public static extern int SetServer(StringBuilder ip, int port);

        #endregion

        #region 通讯延时、时间设置与读取

        /// <summary>
        /// 设置通讯延时
        /// </summary>
        /// <param name="aDelay">等待下位机应答延时</param>
        /// <returns>int</returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Set_TimeOut")]
        public static extern int SetTimeOut(int aDelay);

        /// <summary>
        /// 设置时间  以当前电脑时间更新设备时间
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Set_Time")]
        public static extern int SetTime(StringBuilder ip, int port);

        /// <summary>
        /// 读设备时间  带时钟芯片的才有这个功能
        /// </summary>
        /// <param name="aTime">读出来时间 格式如:2011-08-09 12:23:34</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Read_Time")]
        public static extern int ReadTime(StringBuilder aTime, StringBuilder ip, int port);

        #endregion

        #region IP地址读取、修改

        /// <summary>
        /// 读IP地址
        /// </summary>
        /// <param name="aNetIP">aNetIP 输出参数 保存下位机的通迅参数</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns>int</returns>
        //[DllImport("JX102R.dll", EntryPoint = "JX102R_Read_EquNo")]
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Read_EquNo", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int ReadEquNo(ref T_NetIP aNetIP, StringBuilder ip, int port);

        /// <summary>
        /// 修改IP地址
        /// </summary>
        /// <param name="aNetIP">目标网络地址参数</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Set_Addr")]
        public static extern void SetAddress(T_NetIP aNetIP, StringBuilder ip, int port);

        #endregion

        #region 继电器打开、关闭、延时设置

        /// <summary>
        /// 打开继电器
        /// </summary>
        /// <param name="aChannel">继电器号,可选1,2</param>
        /// <param name="aManualClose">是否手动关闭,True时必须下发关继电器命令，否则对应继电器将常开</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Open_Channel")]
        public static extern int OpenChannel(byte aChannel, bool aManualClose, StringBuilder ip, int port);

        /// <summary>
        /// 关闭继电器
        /// </summary>
        /// <param name="aChannel">继电器号,可选1,2</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Close_Channel")]
        public static extern int CloseChannel(byte aChannel, StringBuilder ip, int port);

        /// <summary>
        /// 设置继电器延时
        /// </summary>
        /// <param name="delay1">继电器1的延时</param>
        /// <param name="delay2">继电器2的延时</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Set_DelayValue")]
        public static extern int SetDelayValue(byte delay1, byte delay2, StringBuilder ip, int port);

        #endregion

        #region 版本号读取

        /// <summary>
        /// 读版本号
        /// </summary>
        /// <param name="verSerial">返出实时门禁板的版本号</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Read_Ver")]
        public static extern int ReadVer(StringBuilder verSerial, StringBuilder ip, int port);

        #endregion

        #region 读取、响应实时卡请求，播报语音，下发显屏信息、响应实时请求并显示LED

        /// <summary>
        /// 读实时卡请求
        /// </summary>
        /// <param name="readerNO">读头编号，如果是1号读头的请求数据，返回1</param>
        /// <param name="cardNO">请求的卡号</param>
        /// <param name="clientIP"></param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Read_Card")]
        public static extern int ReadCard(ref int readerNO, StringBuilder cardNO, StringBuilder clientIP);

        /// <summary>
        /// 响应实时请求
        /// </summary>
        /// <param name="readNO">读头编号</param>
        /// <param name="typeID">响应类型 0为错误  1为正确</param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Rsp_Card")]
        public static extern int RespondCard(int readNO, int typeID);

        /// <summary>
        /// 响应实时请求，并语音播报信息
        /// </summary>
        /// <param name="readNO">读头编号</param>
        /// <param name="sndMsgLen">语音文本长度</param>
        /// <param name="typeID">响应类型 0为错误  1为正确</param>
        /// <param name="sndMsg">要播报的信息</param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_RspSound_Card")]
        public static extern int RespondSoundCard(int readNO, int sndMsgLen, int typeID, StringBuilder sndMsg);

        /// <summary>
        /// 下发顾显屏信息
        /// </summary>
        /// <param name="typeID">类型号(0:全暗 1:单价 2:总计 3:收款 4:找零)</param>
        /// <param name="msg">内容,如：1234.56</param>
        /// <param name="len">内容的长度</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_SetCDMsg")]
        public static extern int SetCDMsg(byte typeID, StringBuilder msg, byte len, StringBuilder ip, int port);

        /// <summary>
        /// 响应实时请求，并在LED屏上显示信息
        /// </summary>
        /// <param name="readNO">读头编号</param>
        /// <param name="typeID">响应类型 0为错误  1为正确</param>
        /// <param name="ledMsg">LED上要显示的信息</param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_RspLed_Card")]
        public static extern int RespondLedCard(int readNO, int typeID, StringBuilder ledMsg);

        #endregion

        #region 设置串口

        /// <summary>
        /// 设置串口波特率
        /// </summary>
        /// <param name="baud1">串口1的波特率</param>
        /// <param name="baud2">串口2的波特率</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Set_UARTBaud")]
        public static extern int SetUARTBaud(int baud1, int baud2, StringBuilder ip, int port);

        /// <summary>
        /// 设置串口接232条码设备时条码的结束标志
        /// </summary>
        /// <param name="len">条码的长度 取值范围 1,2</param>
        /// <param name="end1">条码结束符1	</param>
        /// <param name="end2">条码结束符2</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("JX102R.dll", EntryPoint = "JX102R_Set_CodeEndChar")]
        public static extern int SetCodeEndChar(int len, int end1, int end2, StringBuilder ip, int port);

        #endregion

    }
}
